import JSEncrypt from 'jsencrypt'
export function encrypt(str, publicKey) {
  let jsencrypt = new JSEncrypt();
  jsencrypt.setPublicKey(publicKey);
  const maxLength = 20;
  let encrypteds = [];
  try {
    let subStr = "";
    const strLen = str.length;
    let times = strLen / maxLength;
    if (strLen % maxLength > 0) {
      times++;
    }
    for (let i = 0; i < times; i++) {
      subStr = str.slice(i * maxLength, (i + 1) * maxLength);
      encrypteds.push(jsencrypt.encrypt(subStr));
    }
    return JSON.stringify(encrypteds);
  } catch (ex) {
    return false;
  }
}
